<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use Mail;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use PDO;
use Gregwar\Captcha\CaptchaBuilder;
class HomeLoginController extends Controller
{
    public function getIndex()
    {
    	  // echo '111111';
    	//解析模板
    	 return view('home.login.login');
    }

    public function getUpdate(Request $request)
    {
    	   // dd($request->all());
    	// var_dump($_GET);

    	$name = $request->input('name');

    	 // $res = DB::table('users')->where('username',$name)->select();
    	// $res = DB::table('users')->where('username','shiminghao')->select();
    	$res = DB::select('select * from users where username="'.$name.'"');
    	 // var_dump($res);
    	if($res){
    		echo 0;
    	}else{
    		echo 1;
    	}
    	
   		// echo 1111111;
   	
    }

    public function getYzm(Request $request)
    {
        // dd($request->all());
        //检测验证码是否正确
        if($request->input('name') != session('Vcode')){
            echo 0;
        }else{
            echo 1;
        }
    }



    public function postInsert(Request $request)
    {
    	 // dd($request->all()); 
    	// echo 1111;
    	 $data = $request->only(['username','password','email']);

    	  $data['status'] = 1;
		// //密码加密
		 $data['password'] = Hash::make($data['password']);
         $data['token'] = str_random(50);

         // 执行注册
         $id = DB::table('users')->insertGetId($data);
         if($id){
            //注册成功  发送激活邮件
        // echo 11111111;
            $this->seedmail($id,$data['token'],$request->input('email'));
            return view('home.login.success')->with('success','欢迎登陆火星网,火星不一样的世界');


         }else{
            return back()->with('注册失败','请联系管理员');
         }
		 //插入数据库
		 // $res = DB::table('users')->insert($data);
    	
 		// if($res){
	 	// 	return redirect('/home/goods/index')->with('success','添加成功');
		 // }
		 
		
	    }

        //发送邮件测试
       

 public function seedmail($id,$token,$email)
        {
            // $id = 1;$token='asdgklqw34512334tasdfasd';$email='18735269417@163.com';
            //发送纯文本的字符串1    1
            // Mail::raw('火星网欢迎您,注册请点击,即可获取大量资源,火星网不一样的世界!',function($message){
            //     //设置邮箱标题
            //     $message->subject('激活邮箱');

            //     //发送给谁
            //     $message->to('18735269417@163.com');
            // });

            //第二种
           // 发送其它形式的邮件  可以发送模板
         Mail::send('home.login.email', ['id'=>$id,'token'=>$token,'email'=>$email], function ($message)use($email) {
             $message->to($email)->subject('注册账号激活邮件');
             });
            // return view('home.login.email');
            echo 2222;
        }



        public function getJihuo(Request $request)
        {
            // return '欢迎激活!!!!!!!!!!!!';
            //提取id
            $id= $request->input('id');

            //查询用户
            $user = DB::table('users')->where('id',$id)->first();

            if($user){
                if($request->input('token') == $user->token){
                    $arr = ['status'=>2,'token'=>str_random(50)];
                    if(DB::table('users')->where('id',$id)->update($arr)){
                        // echo '激活成功';
                        return view('home.login.user');
                    }else{
                        return redirect('home.login.email');
                    }
                }else{
                    return redirect('home.login.email');
                }
            }else{
                echo '没有查询到您的信息';
            }
        }

        public function getVcode()
        {
         ob_clean();//清除输出缓存区的内容
        //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        session(['Vcode'=>$phrase]);
        //生成图片
        header("Cache   -Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
        // die();
        }
}
